unit Tellhist;

interface

uses
  Database, Color,
  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  Forms, Dialogs, StdCtrls, Buttons, ExtCtrls, DB, DBTables,
  Grids, Menus, DBGrids, PortalGrid;

type
  TFormTellhist = class(TForm)
    PanelTellBase: TPanel;
    MainMenu: TMainMenu;
    PanelTell: TPanel;
    EditTell: TEdit;
    MenuFile: TMenuItem;
    MenuExit: TMenuItem;
    N2: TMenuItem;
    MenuTell: TMenuItem;
    MenuShowAll: TMenuItem;
    MenuShowOne: TMenuItem;
    MenuClearAll: TMenuItem;
    MenuClearSelected: TMenuItem;
    N3: TMenuItem;
    MenuStayOnTop: TMenuItem;
    MenuExpanded: TMenuItem;
    MenuOptions: TMenuItem;
    MemoTell: TMemo;
    PanelTellhist: TPanel;
    PanelTellhistFrom: TPanel;
    LabelTellhist: TLabel;
    BtnAdd: TSpeedButton;
    MenuInsert: TMenuItem;
    MenuHelp: TMenuItem;
    MenuTellhistHelp: TMenuItem;
    N1: TMenuItem;
    PanelTellgrid: TPanel;
    PanelButtons: TPanel;
    BtnChange: TSpeedButton;
    BtnClearOne: TSpeedButton;
    BtnHelp: TSpeedButton;
    BtnClearAll: TSpeedButton;
    MenuReturn2: TMenuItem;
    PopupMenu1: TPopupMenu;
    PopupColorLine1: TMenuItem;
    PopupMenu2: TPopupMenu;
    PopupColorLine2: TMenuItem;
    PanelPause: TPanel;
    LEDRed2: TImage;
    LEDRed: TImage;
    LEDGreen: TImage;
    BtnPause: TSpeedButton;
    BtnPlay: TSpeedButton;
    TimerPause: TTimer;
    N4: TMenuItem;
    MenuColor: TMenuItem;
    MenuCtrlTab: TMenuItem;
    MenuCtrlTab2: TMenuItem;
    MenuInvisible: TMenuItem;
    MenuAutotyper: TMenuItem;
    MenuChat: TMenuItem;
    MenuStatus: TMenuItem;
    MenuMessage: TMenuItem;
    MenuImagery: TMenuItem;
    MenuFToolbar: TMenuItem;
    MenuPanic: TMenuItem;
    MenuFiltered: TMenuItem;
    MenuVariables: TMenuItem;
    MenuMap: TMenuItem;
    Data1: TMenuItem;
    Canvas1: TMenuItem;
    MenuSave: TMenuItem;
    DialogSave: TSaveDialog;
    N5: TMenuItem;
    N6: TMenuItem;
    CloseandDisableTellMonitor1: TMenuItem;
    DSTell: TDataSource;
    DSTellhist: TDataSource;
    GridTell: TPortalGrid;
    GridTellhist: TPortalGrid;
    procedure GridAllCalcCellColors(Sender: TObject; Field: TField;
      State: TGridDrawState; highlight: Boolean; AFont: TFont;
      ABrush: TBrush);
    procedure FormResize(Sender: TObject);
    procedure GridTellhistCalcCellColors(Sender: TObject; Field: TField;
      State: TGridDrawState; highlight: Boolean; AFont: TFont;
      ABrush: TBrush);
    procedure FormCreate(Sender: TObject);
    procedure EditTellKeyPress(Sender: TObject; var Key: Char);
    procedure GridTellDblClick(Sender: TObject);
    procedure DSTellDataChange(Sender: TObject; Field: TField);
    procedure MenuClearAllClick(Sender: TObject);
    procedure MenuShowAllClick(Sender: TObject);
    procedure MenuShowOneClick(Sender: TObject);
    procedure MenuStayOnTopClick(Sender: TObject);
    procedure MenuExitClick(Sender: TObject);
    procedure DSTellhistDataChange(Sender: TObject; Field: TField);
    procedure MenuExpandedClick(Sender: TObject);
    procedure GridTellhistDblClick(Sender: TObject);
    procedure BtnAddClick(Sender: TObject);
    procedure BtnHelpClick(Sender: TObject);
    procedure BtnChangeClick(Sender: TObject);
    procedure GridTellKeyPress(Sender: TObject; var Key: Char);
    procedure GridTellhistKeyPress(Sender: TObject; var Key: Char);
    procedure MenuReturn2Click(Sender: TObject);
    procedure GridTellCalcCellColors(Sender: TObject; Field: TField;
      State: TGridDrawState; highlight: Boolean; AFont: TFont;
      ABrush: TBrush);
    procedure PopupColorLine1Click(Sender: TObject);
    procedure PopupColorLine2Click(Sender: TObject);
    procedure TimerPauseTimer(Sender: TObject);
    procedure BtnPauseClick(Sender: TObject);
    procedure BtnPlayClick(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure MenuCtrlTabClick(Sender: TObject);
    procedure MenuAutotyperClick(Sender: TObject);
    procedure MenuPanicClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure MenuSaveClick(Sender: TObject);
    procedure CloseandDisableTellMonitor1Click(Sender: TObject);
  private
    { Private declarations }
    procedure WMGetMinMaxInfo(var MSG: Tmessage); message WM_GetMinMaxInfo;
    function FigureFGColor(data: string): TColor;
    function FigureBGColor(data: string): TColor;
    procedure ColorLine(from: string);
  public
    { Public declarations }
    procedure ClosePopup;
  end;

var
  FormTellhist: TFormTellhist;
  lighted, bypass: boolean;

implementation

{$R *.DFM}

uses Main, DBClient;

procedure TFormTellhist.FormCreate(Sender: TObject);
begin
  PanelTellBase.height:=22;
  bypass:=false;
end;

procedure TFormTellhist.WMGetMinMaxInfo(var MSG: Tmessage);
begin
  inherited;
  with PMinMaxInfo(MSG.lparam)^ do begin
    with ptMinTrackSize do begin
      X := 450;
      Y := 147;
    end;
    with ptMaxTrackSize do begin
      X := 9999;
      Y := 9999;
    end;
  end;
end;

procedure TFormTellhist.GridAllCalcCellColors(Sender: TObject;
  Field: TField; State: TGridDrawState; highlight: Boolean; AFont: TFont;
  ABrush: TBrush);
begin
  if Field.fieldname = 'FFrom' then begin
    if copy(Datawell.TableTellhist.fieldbyname('FFrom').asstring,1,3) = 'To:' then begin
      ABrush.color:=clblack;
      AFont.color:=clsilver;
    end;
  end;
end;

procedure TFormTellhist.FormResize(Sender: TObject);
begin
  EditTell.width:=width-21;
end;

procedure TFormTellhist.EditTellKeyPress(Sender: TObject; var Key: Char);
var work: string;
begin
  if key = #13 then begin
    key:=#0;
    if not Datawell.connected then begin
      messagedlg('Not connected to server',mtinformation,[mbok],0);
      exit;
    end;
    if EditTell.text = '' then exit;
    work:=EditTell.text;
    if work[1] = Datawell.TableOptions.fieldbyname('Activator').asstring then begin
      if (length(work) > 1) and (work[2] = Datawell.TableOptions.fieldbyname('Activator').asstring) then begin
        delete(work,1,2);
        FormMain.SendData(work,1);
      end
      else begin
        if Datawell.TableTell.fieldbyname('FFrom').asstring = '' then exit;
        delete(work,1,1);
        FormMain.SendData(work+' '+
          lowercase(Datawell.TableTell.fieldbyname('FFrom').asstring),1);
      end;
    end
    else begin
      if Datawell.TableTell.fieldbyname('FFrom').asstring = '' then exit;
      FormMain.SendData('tell '+
        lowercase(Datawell.TableTell.fieldbyname('FFrom').asstring)+' '+work,1);
    end;
    EditTell.text:='';
  end;
end;

procedure TFormTellhist.GridTellDblClick(Sender: TObject);
begin
  if not MenuShowOne.checked then MenuShowOne.click
  else MenuShowAll.click;
end;

procedure TFormTellhist.DSTellDataChange(Sender: TObject; Field: TField);
begin
  if Datawell.TableTell.fieldbyname('FFrom').asstring = '' then begin
    PopupColorLine1.caption:='&Color tells to/from person';
    PopupColorLine1.enabled:=false;
    MenuColor.caption:='&Color tells to/from person';
    MenuColor.enabled:=false;
    exit;
  end;
  PopupColorLine1.caption:='&Color tells to/from '+Datawell.TableTell.fieldbyname('FFrom').asstring;
  PopupColorLine1.enabled:=true;
  MenuColor.caption:='&Color tells to/from '+Datawell.TableTell.fieldbyname('FFrom').asstring;
  MenuColor.enabled:=true;
  if bypass then exit;
//  if MenuShowOne.checked then MenuShowOne.click;
end;

procedure TFormTellhist.MenuShowAllClick(Sender: TObject);
begin
  with Datawell.TableTellhist do begin
    active:=false;
    filter := 'Master = '+Datawell.masterindex;
    filtered := true;
    active:=true;
    last;
  end;
  MenuShowAll.checked:=true;
  MenuShowOne.checked:=false;
  LabelTellhist.caption:='';
  BtnChange.caption:='One';
  BtnChange.hint:='Show tells from one person';
end;

const
   TELL_FILTER = '(Master = %s) and (FROM1 = "%s")';

procedure TFormTellhist.MenuShowOneClick(Sender: TObject);
var work: string;
begin
  work:=Datawell.TableTell.fieldbyname('FFrom').asstring;
  if work = '' then begin
    MenuShowAll.checked:=true;
    MenuShowOne.checked:=false;
    exit;
  end;
  with Datawell.TableTellhist do begin
    active:=false;
    filter := format(TELL_FILTER, [Datawell.masterindex, work]);
    filtered := true;
    active:=true;
    last;
  end;
  MenuShowOne.checked:=true;
  MenuShowAll.checked:=false;
  LabelTellhist.caption:='Tells to/from: '+work;
  BtnChange.caption:='All';
  BtnChange.hint:='Show tells from all people';
end;

procedure TFormTellhist.MenuStayOnTopClick(Sender: TObject);
begin
  MenuStayOnTop.checked:=not MenuStayOnTop.checked;
  if MenuStayOnTop.checked = true then formstyle:=fsstayontop
  else formstyle:=fsnormal;
end;

procedure TFormTellhist.MenuExitClick(Sender: TObject);
begin
  close;
end;

procedure TFormTellhist.MenuClearAllClick(Sender: TObject);
begin
//  0=All Tells, 1=One Person
  with Datawell.TableTellhist do begin
    if (sender as TComponent).tag = 1 then
      filter := format(TELL_FILTER, [Datawell.masterindex,
                                     Datawell.TableTell.fieldbyname('FFrom').asstring])
    else begin
      filter := 'Master = '+Datawell.masterindex;
    end;
    filtered := true;
    active:=true;
    first;
    while not EOF do delete;
  end;
  if (sender as TComponent).tag = 1 then begin
    if not Datawell.TableTell.EOF then Datawell.TableTell.delete;
  end
  else begin
    with Datawell.TableTell do begin
      first;
      while not EOF do delete;
    end;
    LabelTellhist.caption:='';
  end;
  MenuShowAll.click;
end;

procedure TFormTellhist.DSTellhistDataChange(Sender: TObject;
  Field: TField);
begin
  MemoTell.text:=Datawell.TableTellhist.fieldbyname('Message').asstring;
  if Datawell.TableTellhist.fieldbyname('From1').asstring = '' then begin
    PopupColorLine2.caption:='&Color tells to/from person';
    PopupColorLine2.enabled:=false;
    exit;
  end;
  PopupColorLine2.caption:='&Color tells to/from '+Datawell.TableTellhist.fieldbyname('From1').asstring;
  PopupColorLine2.enabled:=true;
end;

procedure TFormTellhist.MenuExpandedClick(Sender: TObject);
begin
  MenuExpanded.checked:=not MenuExpanded.checked;
  if MenuExpanded.checked = true then begin
    PanelTellbase.height:=58;
  end
  else begin
    PanelTellbase.height:=22;
  end;
end;

procedure TFormTellhist.GridTellhistDblClick(Sender: TObject);
begin
  MenuExpanded.click;
end;

procedure TFormTellhist.BtnAddClick(Sender: TObject);
var
  from: string;
  didfind: boolean;
  table: TClientDataset;
begin
  if length(EditTell.text) < 2 then exit;
  if not Datawell.ValidWord(EditTell.text) then begin
    messagedlg('Not a valid name to insert',mtinformation,[mbok],0);
    exit;
  end;
  from:=LowerCase(EditTell.text);
  from:=UpCase(from[1])+copy(from,2,9999);
  didfind:=false;
  table:=TClientDataset.create(self);
  with table do begin
    CloneCursor(Datawell.TableTells, false);
    filter := 'Master = '+Datawell.masterindex;
    filtered := true;
    active:=true;
    first;
    while not EOF do begin
      if fieldbyname('FFrom').asstring = from then begin
        didfind:=true;
        break;
      end;
      next;
    end;
  end;
  table.free;
  if didfind then exit;
  bypass:=true;
  with Datawell.TableTell do begin
    append;
    fieldbyname('Master').asinteger:=Datawell.masterinteger;
    fieldbyname('FFrom').asstring:=from;
    fieldbyname('Amt').asinteger:=0;
    post;
  end;
  EditTell.text:='';
  if visible then EditTell.setfocus;
  bypass:=false;
  MenuShowOne.click;
end;

procedure TFormTellhist.BtnHelpClick(Sender: TObject);
begin
  Datawell.Help('tell_monitor');
end;

procedure TFormTellhist.BtnChangeClick(Sender: TObject);
begin
  if (sender as TSpeedButton).caption = 'One' then
    MenuShowOne.click else MenuShowAll.click;
end;

procedure TFormTellhist.GridTellKeyPress(Sender: TObject; var Key: Char);
begin
  if key = #13 then begin
    key:=#0;
    GridTellDblClick(Sender);
  end;
end;

procedure TFormTellhist.GridTellhistKeyPress(Sender: TObject;
  var Key: Char);
begin
  if key = #13 then begin
    key:=#0;
    GridTellhistDblClick(Sender);
  end;
end;

procedure TFormTellhist.MenuReturn2Click(Sender: TObject);
begin
  FormMain.show;
end;

procedure TFormTellhist.GridTellCalcCellColors(Sender: TObject;
  Field: TField; State: TGridDrawState; highlight: Boolean; AFont: TFont;
  ABrush: TBrush);
begin
  if Field.fieldname = 'FFrom' then begin
    AFont.color:=FigureFGColor(Datawell.TableTell.fieldbyname('FFrom').asstring);
    AFont.style:=[fsbold];
    ABrush.color:=FigureBGColor(Datawell.TableTell.fieldbyname('FFrom').asstring);
  end;
end;

procedure TFormTellhist.GridTellhistCalcCellColors(Sender: TObject;
  Field: TField; State: TGridDrawState; highlight: Boolean; AFont: TFont;
  ABrush: TBrush);
begin
  if Field.fieldname = 'Index' then begin
    if copy(Datawell.TableTellhist.fieldbyname('FFrom').asstring,1,3) = 'To:' then begin
      ABrush.color:=clblack;
      AFont.color:=clsilver;
    end;
  end
  else if (Field.fieldname = 'FFrom') or (Field.fieldname = 'Message') then begin
    AFont.color:=FigureFGColor(Datawell.TableTellhist.fieldbyname('From1').asstring);
    AFont.style:=[fsbold];
    ABrush.color:=FigureBGColor(Datawell.TableTellhist.fieldbyname('From1').asstring);
  end;
end;

procedure TFormTellhist.PopupColorLine1Click(Sender: TObject);
begin
  ColorLine(Datawell.TableTell.fieldbyname('FFrom').asstring);
end;

procedure TFormTellhist.PopupColorLine2Click(Sender: TObject);
begin
  ColorLine(Datawell.TableTellhist.fieldbyname('From1').asstring);
end;

function TFormTellhist.FigureFGColor(data: string): TColor;
var didfind: boolean;
begin
  didfind:=false;
  with Datawell.TableTells do begin
    first;
    while not EOF do begin
      if fieldbyname('FFrom').asstring = data then begin
        didfind:=true;
        break;
      end;
      next;
    end;
  end;
  if not didfind then begin
    result:=clblack;
    exit;
  end;
  result:=Datawell.TableTells.fieldbyname('FGColor').asinteger;
end;

function TFormTellhist.FigureBGColor(data: string): TColor;
var didfind: boolean;
begin
  didfind:=false;
  with Datawell.TableTells do begin
    first;
    while not EOF do begin
      if fieldbyname('FFrom').asstring = data then begin
        didfind:=true;
        break;
      end;
      next;
    end;
  end;
  if not didfind then begin
    result:=clsilver;
    exit;
  end;
  result:=Datawell.TableTells.fieldbyname('BGColor').asinteger;
end;

procedure TFormTellhist.ColorLine(from: string);
var
  form: TFormColor;
  fi, bi: integer;
  didfind: boolean;
begin
  didfind:=false;
  with Datawell.TableTells do begin
    first;
    while not EOF do begin
      if Datawell.TableTells.fieldbyname('FFrom').asstring = from then begin
        didfind:=true;
        break;
      end;
      next;
    end;
  end;
  form:=TFormColor.create(self);
  if not didfind then begin
    form.ColorGrid.foregroundindex:=0;
    form.ColorGrid.backgroundindex:=7;
  end
  else begin
    form.ColorGrid.foregroundindex:=
      Datawell.ColorFigure(Datawell.TableTells.fieldbyname('FGColor').asinteger);
    form.ColorGrid.backgroundindex:=
      Datawell.ColorFigure(Datawell.TableTells.fieldbyname('BGColor').asinteger);
  end;
  form.showmodal;
  fi:=form.findex;
  bi:=form.bindex;
  form.free;
  if (fi < 0) or (bi < 0) then exit;
  if didfind then Datawell.TableTells.edit else Datawell.TableTells.append;
  Datawell.Tabletells.fieldbyname('Master').asinteger:=Datawell.masterinteger;
  Datawell.TableTells.fieldbyname('FFrom').asstring:=from;
  Datawell.TableTells.fieldbyname('FGColor').asinteger:=Datawell.FigureColor(fi);
  Datawell.TableTells.fieldbyname('BGColor').asinteger:=Datawell.FigureColor(bi);
  Datawell.TableTells.post;
  GridTell.repaint;
  GridTellhist.repaint;
end;

procedure TFormTellhist.TimerPauseTimer(Sender: TObject);
begin
  lighted:=not lighted;
  if lighted then LEDRed.bringtofront else LEDRed2.bringtofront;
end;

procedure TFormTellhist.BtnPauseClick(Sender: TObject);
begin
  lighted:=true;
  LEDRed.bringtofront;
  TimerPause.enabled:=true;
  BtnPause.enabled:=false;
  BtnPlay.enabled:=true;
  Datawell.PauseMessages(1,true);
end;

procedure TFormTellhist.BtnPlayClick(Sender: TObject);
begin
  LEDGreen.bringtofront;
  TimerPause.enabled:=false;
  BtnPause.enabled:=true;
  BtnPlay.enabled:=false;
  Datawell.PauseMessages(1,false);
end;

procedure TFormTellhist.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if key = VK_PAUSE then begin
    if BtnPause.enabled then begin
      BtnPause.click;
      if visible then GridTellhist.setfocus;
    end
    else begin
      BtnPlay.click;
      if visible then EditTell.setfocus;
    end;
    exit;
  end;
end;

procedure TFormTellhist.ClosePopup;
var temp: TWinControl;
begin
  if not active then exit;
  temp:=activecontrol;
  PostMessage(handle,WM_LBUTTONDOWN,MK_LBUTTON,1);
  PostMessage(handle,WM_LBUTTONUP,MK_LBUTTON,1);
  activecontrol:=temp;
end;

procedure TFormTellhist.MenuCtrlTabClick(Sender: TObject);
begin
  if TMenuItem(sender).tag = 0 then FormMain.NextScreen(3)
  else FormMain.PriorScreen(3);
end;

procedure TFormTellhist.MenuAutotyperClick(Sender: TObject);
begin
  FormMain.OpenFloater(TComponent(sender).tag);
end;

procedure TFormTellhist.MenuPanicClick(Sender: TObject);
begin
  FormMain.MenuPanic.click;
end;

procedure TFormTellhist.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  if Datawell.tellspaused then Datawell.PauseMessages(1,false);
  FormMain.UnloadScreen(1);
end;

procedure TFormTellhist.MenuSaveClick(Sender: TObject);
var
  F: TextFile;
  work: string;
  table: TClientDataset;
begin
  DialogSave.initialdir:=Datawell.GetDir(4);
  if not DialogSave.execute then exit;
  Datawell.SetDir(4,DialogSave.filename);
  AssignFile(F,DialogSave.filename);
  Rewrite(F);
  table:=TClientDataset.create(self);
  with table do begin
    CloneCursor(Datawell.TableTellHist, false);
    filtered := true;
    active:=true;
    first;
    work:='All Tells';
    if LabelTellhist.caption <> '' then
      work:='Tells: '+fieldbyname('FFrom').asstring;
    WriteLn(F,work+'  ['+timetostr(time)+'  '+datetostr(date)+']');
    WriteLn(F,'--------');
    while not EOF do begin
      WriteLn(F,fieldbyname('FFrom').asstring+': '+fieldbyname('Message').asstring);
      next;
    end;
  end;
  table.free;
  CloseFile(F);
end;

procedure TFormTellhist.CloseandDisableTellMonitor1Click(Sender: TObject);
begin
  with Datawell.TableOptions3 do begin
    edit;
    fieldbyname('L17').asboolean:=false;
    post;
  end;
  close;
end;

end.
